const directive = {
  bind (el, binding) {
    function documentHandler (e) {
      if (el.contains(e.target)) {
        return false
      }
      if (binding.expression) {
        binding.value(e)
      }
    }
    el._vueClickOutside_ = documentHandler
    document.addEventListener('click', documentHandler)
  },
  unbind (el) {
    document.removeEventListener('click', el._vueClickOutside_)
    delete el._vueClickOutside_
  }
}

export default function install (Vue) {
  Vue.directive('click-outside', directive)
}
